////////////////////////////////////////////////////////////////////////
// BEGIN LICENSE
//
// Copyright (c) 2008 Martin J Bukowski
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
// END LICENSE
////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////
// datafilereader.cpp                                       MJB 11/2/08
//
// contains DataFileReader class implementation
//
////////////////////////////////////////////////////////////////////////

#include "datafilereader.h"
#include "neuralnet_config.h"

#define READBUFFERSIZE 100

#include <sstream>

using namespace std;

////////////////////////////////////////////////////////////////////////
// Begin Class DataFileReader
////////////////////////////////////////////////////////////////////////

// Constructor/Destructor

DataFileReader::DataFileReader(int inputSize, int outputSize, float inputScaling, float outputScaling) 
{
	_inputSize = inputSize;
	_outputSize = outputSize;
	_inputScaling = inputScaling;
	_outputScaling = outputScaling;
}

DataFileReader::~DataFileReader() 
{ 
	if(_fileStream != NULL && _fileStream.is_open())
		_fileStream.close();
}

// Public Fn's

////////////////////////////////////////////////////////////////////////
//-- int OpenFile --
// Input:  string fileName
// Return: int error code
//
//  Opens a file for reading
//
////////////////////////////////////////////////////////////////////////
int DataFileReader::OpenFile(string fileName)
{
	_fileStream.open(fileName.c_str());
	return _fileStream.is_open() ? ERROR_OK : ERROR_FILE_OPEN;
}

////////////////////////////////////////////////////////////////////////
//-- int ReadNextEntry --
// Output: PNData data
// Return: int error code
//
//  Reads a single entry and returns the deserialized NData structure
//
////////////////////////////////////////////////////////////////////////
int DataFileReader::ReadNextEntry(PNData data)
{
	if(!_fileStream.is_open()) return ERROR_FILE_OPEN;

	char cBuffer[READBUFFERSIZE];
	_fileStream.getline(cBuffer, READBUFFERSIZE);
	if(_fileStream.eof()) return ERROR_FILE_EOF;
	string s(cBuffer);
	stringstream ss((const string)s);

	for(int i = 0; i < _inputSize; i++)
	{
		char ciBuffer[20];
		ss.getline(ciBuffer, 20, ',');
		data->input[i] = atof(ciBuffer)/_inputScaling;
	}

	for(int i = 0; i < _outputSize; i++)
	{
		char ciBuffer[20];
		ss.getline(ciBuffer, 20, ',');
		data->output[i] = atof(ciBuffer)/_outputScaling;
	}

	return ERROR_OK;
}

////////////////////////////////////////////////////////////////////////
// End Class DataFileReader
////////////////////////////////////////////////////////////////////////
